﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Apq.IO;
using System.IO;
using System.ComponentModel;

namespace Apq.Windows.Forms
{
	/// <summary>
	/// ComboBox下拉列表助手
	/// </summary>
	public class ComboBoxItemsHelper
	{
		private FsWatcher _fsw = new FsWatcher();
		/// <summary>
		/// 获取内部fsw对象
		/// </summary>
		public FsWatcher fsw
		{
			get { return _fsw; }
		}
		private System.Windows.Forms.ComboBox.ObjectCollection _Items;
		/// <summary>
		/// 获取或设置ComboBox.Items
		/// </summary>
		public System.Windows.Forms.ComboBox.ObjectCollection Items
		{
			set { _Items = value; }
			get { return _Items; }
		}
		private ISynchronizeInvoke _isi;
		/// <summary>
		/// 获取或设置ComboBox所在的窗体
		/// </summary>
		public ISynchronizeInvoke isi
		{
			set { _isi = value; }
			get { return _isi; }
		}

		/// <summary>
		/// ComboBox下拉列表助手,使用前先设置Items和调用者isi
		/// </summary>
		public ComboBoxItemsHelper(System.Windows.Forms.ComboBox.ObjectCollection Items = null, ISynchronizeInvoke isi = null)
		{
			_isi = isi;
			_Items = Items;

			fsw.Created += new System.IO.FileSystemEventHandler(fsw_Created);
			fsw.Deleted += new System.IO.FileSystemEventHandler(fsw_Deleted);
			fsw.Renamed += new System.IO.RenamedEventHandler(fsw_Renamed);
		}

		void fsw_Renamed(object sender, System.IO.RenamedEventArgs e)
		{
			string strOldName = Path.GetFileNameWithoutExtension(e.OldName) ?? string.Empty;
			string strName = Path.GetFileNameWithoutExtension(e.Name) ?? string.Empty;

			Apq.Windows.Delegates.Action_UI<System.Windows.Forms.ComboBox.ObjectCollection>(isi, Items, delegate(System.Windows.Forms.ComboBox.ObjectCollection ctrl)
			{
				int idx = Items.IndexOf(strOldName);
				if (idx > -1)
				{
					Items[idx] = strName;
				}
				else
				{
					Items.Add(strName);
				}
			});
		}

		void fsw_Deleted(object sender, System.IO.FileSystemEventArgs e)
		{
			string strName = Path.GetFileNameWithoutExtension(e.Name);

			Apq.Windows.Delegates.Action_UI<System.Windows.Forms.ComboBox.ObjectCollection>(isi, Items, delegate(System.Windows.Forms.ComboBox.ObjectCollection ctrl)
			{
				Items.Remove(strName);
			});
		}

		void fsw_Created(object sender, System.IO.FileSystemEventArgs e)
		{
			string strName = Path.GetFileNameWithoutExtension(e.Name);

			Apq.Windows.Delegates.Action_UI<System.Windows.Forms.ComboBox.ObjectCollection>(isi, Items, delegate(System.Windows.Forms.ComboBox.ObjectCollection ctrl)
			{
				int idx = Items.IndexOf(strName);
				if (idx == -1)
				{
					Items.Add(strName);
				}
			});
		}

		/// <summary>
		/// 设置监视的文件夹和过滤
		/// </summary>
		public void fsw_Set(string Folder, string Filter = "*.xml")
		{
			bool IsWatch = fsw.FileSystemWatcher.EnableRaisingEvents;
			if (IsWatch)
			{
				fsw.Stop();
			}

			fsw.FileSystemWatcher.Path = Folder;
			fsw.FileSystemWatcher.Filter = Filter;

			if (IsWatch)
			{
				fsw.Start();
			}
		}

		/// <summary>
		/// 重置列表
		/// </summary>
		public void Items_Reset(string Folder, string Filter = "*.xml")
		{
			bool IsWatch = fsw.FileSystemWatcher.EnableRaisingEvents;
			if (IsWatch)
			{
				fsw.Stop();
			}

			Apq.Windows.Delegates.Action_UI<System.Windows.Forms.ComboBox.ObjectCollection>(isi, Items, delegate(System.Windows.Forms.ComboBox.ObjectCollection ctrl)
			{
				Items.Clear();

				string[] aryFiles = Directory.GetFiles(Folder, Filter);
				foreach (string strFile in aryFiles)
				{
					string strFileName = Path.GetFileNameWithoutExtension(strFile);
					if (!Items.Contains(strFileName))
					{
						Items.Add(strFileName);
					}
				}
			});

			if (IsWatch)
			{
				fsw.Start();
			}
		}
	}
}
